<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use app\common\controller\Api;

class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';
    protected $layout = '';
    
    public function index()
    {
        exit;
    }



    public function playchannel()
    {
        $datetime = date('Y-m-d H:i:s');
        // 渠道分组
        $auth_group = Db::name('auth_group')->where('pid',15)->select();
        $channel_group = [];
        foreach ($auth_group as $ag) {
            $channel_group[$ag['name']] = $ag['id'];
        }
        $play = Db::name('play')->where('id IN (190585,190608,190615)')->field('id,channel,shop_no')->select();
        $group = [];
        foreach ($play as $key => $value) {
            $shop = Db::name('shop')->where('shop_no',$value['shop_no'])->field('salesman_admin_id')->find();
            // 活动所属的业务经理,如果group_id为5说明自身是经理，否则找他的上级
            $auth_group_access = Db::name('auth_group_access')->where(['uid'=>$shop['salesman_admin_id'],'group_id'=>5])->find();
            if (!$auth_group_access) {
                $leader = Db::name('admin')->where('id',$shop['salesman_admin_id'])->value('leader');
            } else {
                $leader = $shop['salesman_admin_id'];
            }
            $channel = json_decode($value['channel'],1);
            foreach ($channel as $v) {
                if ($v['channel_id']==3) continue;
                $data = [
                    'channel_id' => $v['channel_id'],
                    'quota' => $v['quota'],
                    'play_id' => $value['id'],
                    'channel_id' => $v['channel_id'],
                    'create_dt' => $datetime,
                    'update_dt' => $datetime,
                    'operate_status' => 1,
                ];
                if ($v['channel_status']=='已分发') {
                    $data['status'] = 1;
                } elseif ($v['channel_status']=='未分发') {
                    $data['status'] = 0;
                } elseif ($v['channel_status']=='无法分发') {
                    $data['status'] = 3;
                } elseif ($v['channel_status']=='无法分发已处理') {
                    $data['status'] = 2;
                } elseif ($v['channel_status']=='已结束') {
                    $data['status'] = 4;
                } else {
                    $data['status'] = 0;
                }
                // 渠道联系人
                $data['connector'] = 0;
                if ($leader && isset($channel_group[$v['channel_name']]) && $channel_group[$v['channel_name']]) {
                    $admin = Db::name('admin')->alias('a')->join('chbc_auth_group_access aga','aga.uid=a.id and aga.group_id='.$channel_group[$v['channel_name']])->where('a.leader2 like "%,'.$leader.',%"')->field('a.id')->find();
                    if ($admin) $data['connector'] = $admin['id'];
                }
                $group[] = $data;
            }
        }
        Db::name('play_channel')->insertAll($group);
    }

}
